import _ from "lodash"
// 数组删除第一个对象
export const removeFirstElement = (arr) => {
    if (Array.isArray(arr) && arr.length > 0) {
        arr.shift();
    }
    return arr;
}

// 数组复制新键值对
export const addKeyWithCopy = (dataArray, sourceKey, targetKey) => {
    return dataArray.map(obj => ({
        ...obj,
        [sourceKey]: obj[targetKey]
    }));
};

// 数组开头添加对象
export const addFirstObject = (arr, newItem) => {
    return [newItem, ...arr]
};

// 数组去重
export const uniqueArray = (arr) => {
    return [...new Set(arr)];
};

// 数组分组
export const groupByKey = (arr, key) => {
    return _.groupBy(arr, key);
};

// 数组过滤空值
export const filterEmptyValues = (arr) => {
    return _.filter(arr, item => !_.isEmpty(item));
};

// 数组排序
export const sortByKey = (arr, key, order = 'asc') => {
    return _.orderBy(arr, [key], [order]);
};

// 数组扁平化
export const flattenArray = (arr) => {
    return _.flattenDeep(arr);
};

